/*
This file is part of AI Agent Feed.

"AI Agent Feed" is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

"AI Agent Feed" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with "AI Agent Feed".  If not, see <http://www.gnu.org/licenses/>.
 */
package com.feed.protocol;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.joda.time.DateTime;

import com.feed.protocol.http.HttpProtocolHandler;

public abstract class AbstractHttpProtocolHandler implements HttpProtocolHandler{
	private static final String PROTOCOL_HTTP = "http";
	int maxTotalConnections;
	int httpport=80;
	HttpClient httpClient;
	String feedURL;
	String encoding ;
	Map<String, String> httpParams = new HashMap<String, String>();
	
	/*public AbstractProtocolHandler(){
		HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        
        // Create and initialize scheme registry 
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(
                new Scheme(PROTOCOL_HTTP, PlainSocketFactory.getSocketFactory(), httpport));
        
        // Create an HttpClient with the ThreadSafeClientConnManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
        httpClient = new DefaultHttpClient(clientConnectionManager, params);
	}*/
	
	public void intialize(){
		HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        
        // Create and initialize scheme registry 
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(
                new Scheme(PROTOCOL_HTTP, PlainSocketFactory.getSocketFactory(), httpport));
        
        // Create an HttpClient with the ThreadSafeClientConnManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
        httpClient = new DefaultHttpClient(clientConnectionManager, params);
	}
	
	public abstract String getContentText();
	
	protected URI constructRequestURL (){
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		for (Iterator<String> paramIterator = httpParams.keySet().iterator(); paramIterator.hasNext();) {
			 String paramKey = paramIterator.next();
			qparams.add(new BasicNameValuePair(paramKey, httpParams.get(paramKey)));
			
		}
		URI uri = null;
		try {
			uri = URIUtils.createURI(PROTOCOL_HTTP, feedURL, -1, "", 
			    URLEncodedUtils.format(qparams, encoding), null);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uri;
	}
	
	public abstract DateTime getLastModified();
	
	public void addParameter (String paramKey,String paramValue){
		httpParams.put(paramKey, paramValue);
	}
	
	public void removeParameter (String paramKey){
		httpParams.remove(paramKey);
	}
	
	public void decompose(){
		 httpClient.getConnectionManager().shutdown();    
	}

	public int getMaxTotalConnections() {
		return maxTotalConnections;
	}

	public void setMaxTotalConnections(int maxTotalConnections) {
		this.maxTotalConnections = maxTotalConnections;
	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public String getFeedURL() {
		return feedURL;
	}

	public void setFeedURL(String feedURL) {
		this.feedURL = feedURL;
	}

	public int getHttpport() {
		return httpport;
	}

	public void setHttpport(int httpport) {
		this.httpport = httpport;
	}
	
	
}
